<?php

// +----------------------------------------------------------------------
// | Think.Admin
// +----------------------------------------------------------------------
// | 版权所有 2014-2018 东莞市云拓互联网络科技有限公司
// +----------------------------------------------------------------------
// | 官方网站:http://www.ytclouds.net
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/Think.Admin
// +----------------------------------------------------------------------

namespace app\webapi\controller;

use app\webapi\controller\Baseapp;
use app\webapi\service\AppuserService;
use service\DataService;
use app\webapi\service\LotteryService;
use think\Db;

/**
 * @package app\store\controller
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/03/27 14:43
 */
class Lottery extends Baseapp
{

    /**
     * 抽奖首页
     * @return mixed
     */
    public function index()
    {
        $user_info = AppuserService::get_user_data($this->user_id, "*");
        $get = $this->request->get();

        /**
         * 抽奖类型
         */
        $type=isset($get['type']) && $get['type'] ? $get['type'] : 0;

        return $this->fetch('', [
            'user_info' => $user_info,
            'url'=>'http://'.$_SERVER['HTTP_HOST'],
            'type'=>$type
        ]);
    }

    /**
     * 获取抽奖列表
     */
    public function get_lottery_list()
    {
        $get = $this->request->get();
        /**
         * 抽奖类型
         */
        $type=isset($get['type']) && $get['type'] ? $get['type'] : 0;
        $status = $this->success_code;
        $msg = '抽奖奖品列表';
        $lottery_list = [];
        //获取抽奖规则
        $lottery_list = LotteryService::get_rule($lottery_list,$type);
        //获取中奖列表
        $pool = LotteryService::get_pool($type);
        if (!$pool) {
            $status = 2;
            $msg = '暂时没有奖品';
        }
        $lottery_list['poolList'] = $pool;
        //获取中奖者，最近30条
        $lottery_list = LotteryService::get_winners($lottery_list,$type);
        $arr = [
            'status' => $status,
            'msg' => $msg,
            'result' => $lottery_list
        ];
        $this->json_return($arr);
    }

    /**
     * 开始抽奖
     */
    public function play()
    {
        $get = $this->request->get();
        /**
         * 抽奖类型
         */
        $type=isset($get['type']) && $get['type'] ? $get['type'] : 0;
        $lottery_list = isset($get['lottery_list']) ? json_decode(htmlspecialchars_decode($get['lottery_list']), true) : null;
        $res = LotteryService::play($this->user_id, $lottery_list,$type);
        $this->json_return($res);
    }

}
